package com.mtw.bbs.article.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mtw.bbs.article.mapper.TopicIndexMapper;
import com.mtw.bbs.article.mapper.TopicTypeMapper;
import com.mtw.bbs.article.pojo.entity.TopicIndex;
import com.mtw.bbs.article.pojo.entity.TopicType;
import com.mtw.bbs.article.pojo.form.TopicTypeForm;
import com.mtw.bbs.article.pojo.vo.TopicTypeVo;
import com.mtw.bbs.article.service.TopicTypeService;
import com.mtw.bbs.common.core.constant.ServiceConstant;
import com.mtw.bbs.common.core.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;


@RequiredArgsConstructor
@Service("topicTypeService")
public class TopicTypeServiceImpl extends ServiceImpl<TopicTypeMapper, TopicType> implements TopicTypeService {


    private final TopicIndexMapper topicIndexMapper;

    /**
     * 获取所有启用的话题分类
     */
    @Override
    public List<TopicTypeVo> getAllEnableType(){

        LambdaQueryWrapper<TopicType> lqw = new LambdaQueryWrapper<>();
        lqw.select(TopicType::getId,TopicType::getName);
        lqw.eq(TopicType::getStatus, ServiceConstant.STATUS_ENABLE);
        lqw.orderByDesc(TopicType::getSort);
        return list(lqw).stream().map(x->{
            TopicTypeVo vo = new TopicTypeVo();
            vo.setId(x.getId());
            vo.setName(x.getName());
            return vo;
        }).collect(Collectors.toList());
    }




    /**
     * 保存话题分类
     *
     * @param form 话题分类表单
     * @return 话题主键
     */
    @Override
    public boolean saveTopicType(TopicTypeForm form){

        TopicType entity = new TopicType();
        BeanUtils.copyProperties(form,entity);
        return save(entity);
    }

    /**
     * 更新话题分类
     *
     * @param form 话题分类表单
     * @return 更新结果
     */
    @Override
    public  boolean updateTopicType(TopicTypeForm form){
        TopicType entity = new TopicType();
        BeanUtils.copyProperties(form,entity);
        return updateById(entity);
    }


    /**
     * 删除话题分类
     *
     * @param id 话题分类id
     * @return 删除结果
     */
    @Override
    public  boolean deleteTopicType(String id){

        Long count = topicIndexMapper.selectCount(new LambdaQueryWrapper<TopicIndex>().eq(TopicIndex::getTypeId, id));
        if (count > 0){
            throw new BusinessException("存在关联的话题");
        }
        return removeById(id);
    }


    /**
     * 删除话题分类
     *
     * @param ids 话题分类id集合
     * @return 删除结果
     */
    @Override
    @Transactional
    public boolean deleteTopicType(Collection<String> ids) {

        if (CollUtil.isEmpty(ids)){
            return false;
        }
        Long count = topicIndexMapper.selectCount(new LambdaQueryWrapper<TopicIndex>().in(TopicIndex::getTypeId, ids));
        if (count > 0){
            throw new BusinessException("存在关联的话题");
        }
        return removeBatchByIds(ids);
    }

}
